var globel = {}
const barPath = './bar.js'
var installMap = {}

const createScriptWitPath = (path) => {
  const script = document.createElement('script')
  script.src = path
  document.head.appendChild(script)
}
const require = (path) => {
  return globel[path]
}
const btn = document.createElement('button')
btn.textContent = '点击我'
document.body.appendChild(btn)

btn.addEventListener('click', () => {
  Promise.resolve(
    new Promise((res, rej) => {
      if (globel[barPath]) {
        res()
      } else {
        createScriptWitPath(barPath)
        globel[barPath] = [res, rej]
      }
    })
  )
    .then(function () {
      return require(barPath)
    })
    .then(function (res) {
      console.log(res)
    })
    ['catch'](function (err) {
      console.log(err)
    })
})
